//
//  OPBaseObject.h
//  OPFramework
//
//  Created by malcom on 7/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XMLRPC.h"
#import "OPResultsKeys.h"
#import "OPCostants.h"

@class OPBaseObject;

/*!
    @protocol		OPObject_AsyncResponse
    @abstract		This protocol must be implemented in each listener of a OPBaseObject class (or derivated classes). <br/>
    @discussion		You should implement this protocol when you are using OFAPI framework in async mode. <br/>
					You will be alerted when a class is updated (<i>-dataUpdatedFor: with the updated object) <br/>
					or an error is occurred (function error is owned by <i>-errorReceivedForDataUpdate:</i> method that contains <br/>
					the error code [see OPCostants.h], while standard connection/xml/parsing errors are notified by <br/>
					<i>-serverErrorForDataUpdate</i>)<br/>
*/
@protocol OPObject_AsyncResponse
- (void) dataUpdatedFor:(OPBaseObject *) _obj atMethod:(NSString *) _method;					// object data was updated successfully
- (void) errorReceivedForDataUpdate:(NSNumber *) _errorCode from:(OPBaseObject *) _obj;			// a numeric error value was returned
- (void) serverErrorForDataUpdateFrom:(OPBaseObject *) _obj error:(NSDictionary *) _dict;		// an internal error / ofapi error has occurred
@end

@protocol XMLRPC_AsyncResponse
- (void)connection: (XMLRPCConnection *)connection didReceiveResponse:(XMLRPCResponse *)response forMethod: (NSString *)method;
@end

@class OPBoard;
@interface OPBaseObject : NSObject <XMLRPC_AsyncResponse,OPObject_AsyncResponse> {
	OPBoard			*_delegateBoard;
	XMLRPCResponse	*_lastResponse;
	NSDate			*_lastUpdate;
	NSMutableArray	*_listeners;
	int				_lastErrorCode;
	BOOL			_asyncModeActive;
}

@property (assign) OPBoard			*_delegateBoard;
@property (assign) XMLRPCResponse	*_lastResponse;
@property (assign) NSDate			*_lastUpdate;
@property (readonly) int			_lastErrorCode;
@property (assign) BOOL				_asyncModeActive;

#pragma mark INIT
- (id) initWithBoard:(OPBoard *) _board;
- (id) initWithBoard:(OPBoard *) _board andListener:( NSObject <OPObject_AsyncResponse> *) _listener;

- (BOOL) loadData;
- (BOOL) dataAvailable;
- (void) addObjectAsListenerForMe:(NSObject <OPObject_AsyncResponse>*) _resp;
- (void) dataUpdatedFor:(OPBaseObject *) _obj atMethod:(NSString *) _method;
- (BOOL) isCacheExpired;
- (void) sendXMLRequestForMethod:(NSString *) _methodName withParams:(NSArray *) _params parseSelector:(SEL) _parseSel;
- (BOOL) sendXMLRequestForMethod:(NSString *) _methodName withParams:(NSArray *) _params parseSelector:(SEL) _parseSel forceSyncRequest:(BOOL) _sync;
- (void) refreshData;
- (void) setCacheAsExpired; // use this if you want to reload data from server without using cache

#pragma mark PRIVATE METHODS
- (BOOL) _isNumericResponseFromServerAnErrorCode:(NSNumber *) _code forMethod:(NSString *) method;
- (BOOL) _checkErrorInRawResponseFromServer:(XMLRPCResponse *) response forMethod:(NSString *) method;

@end
